home *** CD-ROM | disk | FTP | other *** search
- Path: dante.network-1.com!garrett
- Newsgroups: comp.lang.c++
- Subject: Re: Help: Containers for templated classes
- Message-ID: <1996Mar22.020605.6@network-1.com>
- From: garrett@I_should_put_my_domain_in_etc_NNTP_INEWS_DOMAIN (Don Garrett)
- Date: 22 Mar 96 02:06:01 -0600
- References: <314740D4.23B1@mit.edu>
- Organization: Network-1
- Nntp-Posting-Host: dante.network-1.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Imran Haq (ihaq@mit.edu) wrote:
- : I'm kind of new to advanced usage of templates. Does anyone know how
- : to put different template classes in a single container. Is this possible?
- : For example, I have instations
-
- : myClass<int> *type1;
- : myClass<double> *type;
-
- : ....
-
- : I'm using STL to provide container classes:
-
- : vector<myClass<????> > container;
-
- : But this obviously does not work. Does anyone know about any
- : workarounds?? HELP!!!
-
-
- : Imran Haq
- : ihaq@mit.edu
-
- Of course it depends on what you are wanting to do, but here is a
- solution.
-
- class myBase;
-
- template myClass<T> : public myBase {}
-
- Then use a container that holds POINTERS to myBase. Use virtual
- functions to make sure that appropriate type variant functions are
- used on myClass.
-
- The reason you can't put myClass<int> and myClass<double> in the
- same vector is that they are different types. They must be different
- because the interfaces to these classes (and even the size of an
- instance of the class) can vary. By using myBase, the interface
- (and the type) don't vary.
-
- --
- Don Garrett http://www.network-1.com/~garrett/
- garrett@network-1.com
-